/**
 *
 */
package vn.tecviets.thuandt.model.bo;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

import vn.tecviets.thuandt.model.bean.Post;
import vn.tecviets.thuandt.model.common.Constant;
import vn.tecviets.thuandt.model.common.HTMLFile;
import vn.tecviets.thuandt.model.dao.PostDAO;

/*
 * PostBO
 * 1.0
 * Mar 19, 2013
 * 
 * Copyright notice
 * 
 * Modification Logs:
 * 
 * DATE					AUTHOR				DESCRIPTION
 * -----------------------------------------------------------
 * Mar 19, 2013			Thuandt				Create
 */

public class PostBO {

	PostDAO postDAO = new PostDAO();
	HTMLFile htmlFile = new HTMLFile();

	/**
	 * Get given quantity posts (Get Title, IsShowed, IsImportant, CreatedDate,
	 * ModifiedDate, CategoryTitle, Username, CategoryId)
	 * 
	 * @param pageIndex
	 *            (firstPage - 1, secondPage - 2, ...)
	 * @return If don't have any post, return null.
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public ArrayList<Post> getPostForPaging(int pageIndex) throws ClassNotFoundException, SQLException {

		ArrayList<Post> postArrayList = new ArrayList<Post>();

		int from = Constant.POST_PER_PAGE * (pageIndex - 1);
		int to = from + Constant.POST_PER_PAGE;

		try {

			postArrayList = postDAO.selectPostForPaging(from, to);

		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}

		return postArrayList;
	}

	/**
	 * Get total of pages
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public int getTotalPage() throws ClassNotFoundException, SQLException {

		int totalPage = 0;

		try {

			int totalPosts = postDAO.countTotalPost();
			totalPage = totalPosts / Constant.POST_PER_PAGE;
			if (totalPosts % Constant.POST_PER_PAGE != 0) {
				totalPage++;
			}

		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}

		return totalPage;
	}

	/**
	 * Delete a post
	 * 
	 * @param postId
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public String deletePost(int postId) throws ClassNotFoundException, SQLException {
		String pathname = null;
		
		try {
			pathname = postDAO.deletePost(postId);
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}
		
		return pathname;
	}

	/**
	 * Validate information of a new post
	 * 
	 * @param content
	 * @param summary
	 * @param categoryTitle
	 * @param postName
	 * @param userID
	 * @param password
	 * @param role
	 * @return
	 */
	public ActionErrors validateCreate(String title, int categoryId, String image, String caption, String summary,
			String content, String pathname) {

		ActionErrors actionErrors = new ActionErrors();

		if ("".equals(title.trim())) {
			actionErrors.add("titleMessageError", new ActionMessage("error.emptyTitle"));
		} else if (title.length() > Constant.TITLE_MAX_LENGTH) {
			actionErrors.add("titleMessageError", new ActionMessage("error.longTitle", Constant.TITLE_MAX_LENGTH));
		} else if (htmlFile.isFileExisted(pathname)) {
			actionErrors.add("titleMessageError", new ActionMessage("error.existTitle"));
		}

		if (categoryId == 0) {
			actionErrors.add("categoryMessageError", new ActionMessage("error.notSelectedCategory"));
		}

		if ("".equals(image) && !"".equals(caption)) {
			actionErrors.add("imageMessageError", new ActionMessage("error.emptyImage"));
		}

		if (!"".equals(image) && "".equals(caption)) {
			actionErrors.add("captionMessageError", new ActionMessage("error.emptyCaption"));
		}

		if ("".equals(summary)) {
			actionErrors.add("summaryMessageError", new ActionMessage("error.emptySummary"));
		} else if (summary.length() > Constant.SUMMARY_MAX_LENGTH) {
			actionErrors
					.add("summaryMessageError", new ActionMessage("error.longSummary", Constant.SUMMARY_MAX_LENGTH));
		}

		if ("".equals(content)) {
			actionErrors.add("contentMessageError", new ActionMessage("error.emptyContent"));
		}

		return actionErrors;
	}

	/**
	 * Create a new post
	 * 
	 * @param title
	 * @param categoryId
	 * @param image
	 * @param caption
	 * @param summary
	 * @param showed
	 * @param important
	 * @param username
	 * @param content
	 * @param pathFolder
	 * @throws ClassNotFoundException
	 * @throws IOException
	 * @throws SQLException
	 */
	public void createPost(String title, int categoryId, String image, String caption, String summary,
			String showed, String important, String username, String content, String pathname)
			throws ClassNotFoundException, IOException, SQLException {

		Post post = new Post();
		post.setTitle(title);
		post.setCategoryId(categoryId);
		post.setImage(image);
		post.setCaption(caption);
		post.setSummary(summary);
		post.setShowed(showed);
		post.setImportant(important);
		post.setPathname(pathname);
		post.setUsername(username);

		try {
			htmlFile.createFile(content, pathname);
			postDAO.insertPost(post);
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}
	}

	/**
	 * Get a post (Title, CategoryTitle, Image, Caption, Summary, IsShowed,
	 * IsImportant, Pathname, CategoryId)
	 * 
	 * @param postID
	 * @return a post
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Post getPostByID(int postID) throws ClassNotFoundException, SQLException {
		Post post = null;

		try {
			post = postDAO.selectPostByID(postID);
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}

		return post;
	}

	/**
	 * @param postId
	 * @param title
	 * @param categoryId
	 * @param image
	 * @param caption
	 * @param summary
	 * @param showed
	 * @param important
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public void updatePost(int postId, String title, int categoryId, String image, String caption, String summary,
			String showed, String important) throws ClassNotFoundException, SQLException {
		Post post = new Post();
		post.setPostId(postId);
		post.setTitle(title);
		post.setCategoryId(categoryId);
		post.setImage(image);
		post.setCaption(caption);
		post.setSummary(summary);
		post.setShowed(showed);
		post.setImportant(important);
		
		try {
			postDAO.updatePost(post);
		} catch (ClassNotFoundException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}
	}

	/**
	 * @param categoryId
	 * @param image
	 * @param caption
	 * @param summary
	 * @param content
	 * @return
	 */
	public ActionErrors validateUpdate(String image, String caption, String summary, String content) {
		
		ActionErrors actionErrors = new ActionErrors();

		if ("".equals(image) && !"".equals(caption)) {
			actionErrors.add("imageMessageError", new ActionMessage("error.emptyImage"));
		}

		if (!"".equals(image) && "".equals(caption)) {
			actionErrors.add("captionMessageError", new ActionMessage("error.emptyCaption"));
		}

		if ("".equals(summary)) {
			actionErrors.add("summaryMessageError", new ActionMessage("error.emptySummary"));
		} else if (summary.length() > Constant.SUMMARY_MAX_LENGTH) {
			actionErrors
					.add("summaryMessageError", new ActionMessage("error.longSummary", Constant.SUMMARY_MAX_LENGTH));
		}

		if ("".equals(content)) {
			actionErrors.add("contentMessageError", new ActionMessage("error.emptyContent"));
		}

		return actionErrors;
	}

}
